#include <graphics.h>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <windows.h>

#define PI 3.14159265358979323846

void drawBaseDashboard(float centerX, float centerY, float radius) // 表盘界面(比例化版本)
{
	ege_enable_aa(true);
// 计算比例因子 (基于原始radius=280的比例)
	const float scale = radius / 280.0f;
	
// 比例化参数
	const int sechang = static_cast<int>(20 * scale);
	const float outer_ring1 = radius + 27 * scale;
	const float outer_ring2 = radius + 20 * scale;
	const float outer_ring3 = radius + 17 * scale;
	const float inner_ring = radius - sechang;
	const float mid_ring = radius - sechang/2;
	
	cleardevice();
	setcolor(EGERGB(110, 110, 110));
// 绘制一个圆
	ege_circle(centerX, centerY, radius);
// ==================== 第一层外环 ====================
	setcolor(EGERGB(30, 30, 30));
	setfillcolor(EGERGB(30, 30, 30));
	ege_fillpie(centerX - outer_ring1, centerY - outer_ring1, outer_ring1 * 2, outer_ring1 * 2, 0, 360, NULL);
	
	for(int i = 0; i <= 30; i++) {
		setcolor(EGERGB(20+i, 20+i, 20+i));
		setfillcolor(EGERGB(20+i, 20+i, 20+i));
		ege_fillpie(centerX - outer_ring1, centerY - outer_ring1, outer_ring1 * 2, outer_ring1 * 2, 240 + i*2, 10, NULL);
	}
	
	for(int i = 0; i <= 30; i++) {
		setcolor(EGERGB(20+i, 20+i, 20+i));
		setfillcolor(EGERGB(20+i, 20+i, 20+i));
		ege_fillpie(centerX - outer_ring1, centerY - outer_ring1, outer_ring1 * 2, outer_ring1 * 2, 370 - i*2, 10, NULL);
	}
	
// ==================== 第二层外环 ====================
	setcolor(EGERGB(10, 10, 10));
	setfillcolor(EGERGB(10, 10, 10));
	ege_fillcircle(centerX, centerY, outer_ring2);
	
	for(int i = 0; i <= 30; i++) {
		const float r1 = outer_ring2 - 2;
		setcolor(EGERGB(80+i, 80+i, 80+i));
		setfillcolor(EGERGB(80+i, 80+i, 80+i));
		ege_fillpie(centerX - r1, centerY - r1, r1 * 2, r1 * 2, 30 + i*2, 10, NULL);
	}
	
	for(int i = 0; i <= 30; i++) {
		const float r2 = outer_ring2 - 1;
		setcolor(EGERGB(80+i, 80+i, 80+i));
		setfillcolor(EGERGB(80+i, 80+i, 80+i));
		ege_fillpie(centerX - r2, centerY - r2, r2 * 2, r2 * 2, 160 - i*2, 10, NULL);
	}
	
// ==================== 第三层外环 ====================
	setcolor(EGERGB(20, 20, 20));
	setfillcolor(EGERGB(20, 20, 20));
	ege_fillpie(centerX - outer_ring3, centerY - outer_ring3, outer_ring3 * 2, outer_ring3 * 2, 0, 360, NULL);
	
	for(int i = 0; i <= 30; i++) {
		setcolor(EGERGB(20+i, 20+i, 20+i));
		setfillcolor(EGERGB(20+i, 20+i, 20+i));
		ege_fillpie(centerX - outer_ring3, centerY - outer_ring3, outer_ring3 * 2, outer_ring3 * 2, 240 + i*2, 10, NULL);
	}
	
	for(int i = 0; i <= 30; i++) {
		setcolor(EGERGB(20+i, 20+i, 20+i));
		setfillcolor(EGERGB(20+i, 20+i, 20+i));
		ege_fillpie(centerX - outer_ring3, centerY - outer_ring3, outer_ring3 * 2, outer_ring3 * 2, 370 - i*2, 10, NULL);
	}
	
// ==================== 渐变效果环 ====================
	for(int i = 0; i <= 30; i++) {
		const float cr = radius + i * 0.14f * scale;
		setcolor(EGERGB(60+i, 60+i, 60+i));
		setfillcolor(EGERGB(60+i, 60+i, 60+i));
		ege_fillpie(centerX - cr, centerY - cr, cr*2, cr*2, 240 + i*2, 10, NULL);
	}
	
	for(int i = 0; i <= 30; i++) {
		const float cr = radius + i * 0.14f * scale;
		setcolor(EGERGB(60+i, 60+i, 60+i));
		setfillcolor(EGERGB(60+i, 60+i, 60+i));
		ege_fillpie(centerX - cr, centerY - cr, cr*2, cr*2, 370 - i*2, 10, NULL);
	}	
	
// 中心环（改为fillcircle）
	setcolor(EGERGB(10, 10, 10));
	setfillcolor(EGERGB(10, 10, 10));
	ege_fillcircle(centerX, centerY, radius + 2 * scale);  // 替换原sector
	
// 最内环（改为fillcircle）
	setcolor(EGERGB(35, 36, 35));
	setfillcolor(EGERGB(35, 36, 35));
	ege_fillcircle(centerX, centerY, radius);  // 替换原sector
}


void drawScaleNum(int centerX, int centerY, int radius)
{
	// 预计算数字位置
	int numPos[4][2] = {0};
	for (int i = 0; i < 4; i++) 
	{
		double angle = 120 - i * 20;
		double rad = angle * PI / 180;
		numPos[i][0] = centerX + (radius - radius*0.233) * cos(rad) - radius*0.053;
		numPos[i][1] = centerY - (radius - radius*0.233) * sin(rad) - radius*0.053;
	}
	// 绘制刻度
	for (int i = 1; i <= 17; i++) 
	{
		double angle = 135 - i * 5;
		double rad = angle * PI / 180;
		int len = 0;
		if(i == 3 || i == 7 || i == 11 || i == 15)
		{
			len = radius / 6;
			setlinestyle(SOLID_LINE, 0, radius/30);
		}
		else if(i % 2 == 1)
		{
			len = radius*0.1;
			setlinestyle(SOLID_LINE, 0, radius*0.02);
		}
		else
		{
			len = radius / 15;
			setlinestyle(SOLID_LINE, 0, radius*0.014);
		}
		setcolor(WHITE);
		ege_line(
			centerX + (radius - len) * cos(rad),
			centerY - (radius - len) * sin(rad),
			centerX + radius * cos(rad),
			centerY - radius * sin(rad));
	}	
	// 绘制数字
	setbkmode(TRANSPARENT);  // 设置文字背景透明	
	setcolor(WHITE);
	setfont(radius / 6, 0, "SimHei");
	for (int i = 0; i <= 3; i++)
	{
		char str[3];
		sprintf(str, "%d", i+3);
		xyprintf(numPos[i][0], numPos[i][1], "%s", str);
	}	
}

void rotate_point(int cx, int cy, int x, int y, float angle, int* outX, int* outY)// 旋转坐标计算函数
{
	// 将点平移到原点
	x -= cx;
	y -= cy;	
	// 计算旋转后的坐标
	*outX = cx + (int)(x * cos(angle) - y * sin(angle));
	*outY = cy + (int)(x * sin(angle) + y * cos(angle));
}

void drawPointer(int cx, int cy, int radius, float angle)// 绘制旋转指针
{
	// 计算比例因子 (基于原始设计为150半径的表盘)
	const float scale = radius / 150.0f;	
	// 指针各部分尺寸定义
	float pointer_width = 17.0f * scale;    // 指针宽度
	const float pointer_length = 140.0f * scale;  // 指针总长度
	const float pointer_tail_length = 50.0f * scale; // 指针尾部长度
	const float pointer_tip_length = 25.0f * scale;  // 指针尖端超出主体的长度	
	// 指针各部分坐标（比例化后）
	// 指针主体矩形坐标
	int rect_left = cx - static_cast<int>(pointer_width/2);
	int rect_top = cy - static_cast<int>(pointer_length - pointer_tip_length) + radius / 20;
	int rect_right = cx + static_cast<int>(pointer_width/2);
	int rect_bottom = cy - static_cast<int>(pointer_tip_length) + radius / 20;
	// 指针三角形坐标
	int tri_points[3][2] = 
	{
		{cx - static_cast<int>(pointer_width/2), cy - static_cast<int>(pointer_length - pointer_tip_length) + radius / 20+1},
		{cx + static_cast<int>(pointer_width/2), cy - static_cast<int>(pointer_length - pointer_tip_length) + radius / 20+1} ,
		{cx, cy - static_cast<int>(pointer_length) + radius / 20+1} 
	};
	
	pointer_width = 7.0f * scale;    // 指针宽度
	// 指针底部矩形坐标
	int bottom_rect_left = cx - static_cast<int>(pointer_width/2);
	int bottom_rect_top = cy;
	int bottom_rect_right = cx + static_cast<int>(pointer_width/2);
	int bottom_rect_bottom = cy - static_cast<int>(pointer_tip_length);
	
	// 指针尾部坐标
	int tail_rect_left = cx - static_cast<int>(pointer_width/2);
	int tail_rect_top = cy;
	int tail_rect_right = cx + static_cast<int>(pointer_width/2);
	int tail_rect_bottom = cy + static_cast<int>(pointer_tail_length);
	
	// 旋转后的坐标
	int rotated_rect[4][2];
	int rotated_tri[3][2];
	int rotated_bottom_rect[4][2];
	int rotated_tail_rect[4][2];
	
	// 计算旋转后的矩形坐标
	rotate_point(cx, cy, rect_left, rect_top, angle, &rotated_rect[0][0], &rotated_rect[0][1]);
	rotate_point(cx, cy, rect_right, rect_top, angle, &rotated_rect[1][0], &rotated_rect[1][1]);
	rotate_point(cx, cy, rect_right, rect_bottom, angle, &rotated_rect[2][0], &rotated_rect[2][1]);
	rotate_point(cx, cy, rect_left, rect_bottom, angle, &rotated_rect[3][0], &rotated_rect[3][1]);
	
	// 计算旋转后的三角形坐标
	for (int i = 0; i < 3; i++)
	{
		rotate_point(cx, cy, tri_points[i][0], tri_points[i][1], angle, 
			&rotated_tri[i][0], &rotated_tri[i][1]);
	}
	
	// 绘制指针底部（深色部分）
	setfillcolor(EGERGB(17,14,11));
	setcolor(EGERGB(17,14,11));
	
	// 计算并绘制底部矩形
	rotate_point(cx, cy, bottom_rect_left, bottom_rect_top, angle, 
		&rotated_bottom_rect[0][0], &rotated_bottom_rect[0][1]);
	rotate_point(cx, cy, bottom_rect_right, bottom_rect_top, angle, 
		&rotated_bottom_rect[1][0], &rotated_bottom_rect[1][1]);
	rotate_point(cx, cy, bottom_rect_right, bottom_rect_bottom, angle, 
		&rotated_bottom_rect[2][0], &rotated_bottom_rect[2][1]);
	rotate_point(cx, cy, bottom_rect_left, bottom_rect_bottom, angle, 
		&rotated_bottom_rect[3][0], &rotated_bottom_rect[3][1]);
	
	ege_point bottom_points[4];
	for (int i = 0; i < 4; i++)
	{
		bottom_points[i].x = rotated_bottom_rect[i][0];
		bottom_points[i].y = rotated_bottom_rect[i][1];
	}
	ege_fillpoly(4, bottom_points);
	
	// 计算并绘制尾部矩形
	rotate_point(cx, cy, tail_rect_left, tail_rect_top, angle, 
		&rotated_tail_rect[0][0], &rotated_tail_rect[0][1]);
	rotate_point(cx, cy, tail_rect_right, tail_rect_top, angle, 
		&rotated_tail_rect[1][0], &rotated_tail_rect[1][1]);
	rotate_point(cx, cy, tail_rect_right, tail_rect_bottom, angle, 
		&rotated_tail_rect[2][0], &rotated_tail_rect[2][1]);
	rotate_point(cx, cy, tail_rect_left, tail_rect_bottom, angle, 
		&rotated_tail_rect[3][0], &rotated_tail_rect[3][1]);
	
	ege_point tail_points[4];
	for (int i = 0; i < 4; i++)
	{
		tail_points[i].x = rotated_tail_rect[i][0];
		tail_points[i].y = rotated_tail_rect[i][1];
	}
	ege_fillpoly(4, tail_points);
	
<<<<<<< HEAD
	
=======

>>>>>>> 4f7d5b874e40f4bb9b0a9275838d6f2d563fcd79
	
// 绘制指针底部（深色部分）
	const float scale1 = radius / 240.0f;
	setcolor(EGERGB(30, 30, 30));
	setfillcolor(EGERGB(30, 30, 30));
	ege_fillcircle(cx, cy,  30 * scale1);  
	
	
	for(int i = 0; i <= 45; i++) 
	{
		setcolor(EGERGB(30+i, 30+i, 30+i));
		setfillcolor(EGERGB(30+i, 30+i, 30+i));
		ege_fillpie(cx - 30 * scale1,cy - 30 * scale1,
			60 * scale1,60 * scale1,               
			90 + i*2,10,NULL);
	}
	
	for(int i = 0; i <= 45; i++) 
	{
		setcolor(EGERGB(30+i, 30+i, 30+i));
		setfillcolor(EGERGB(30+i, 30+i, 30+i));
		ege_fillpie(
			cx - 30 * scale1,cy - 30 * scale1,
			60 * scale1,60 * scale1,
			260 -i*2,10,NULL);
	}
	
	setcolor(EGERGB(30, 30, 30));
	setfillcolor(EGERGB(30, 30, 30));
	ege_fillcircle(cx, cy,  20 * scale1);
	
	// 绘制尾部圆点
	int tail_circle_x, tail_circle_y;
	rotate_point(cx, cy, cx, cy + static_cast<int>(pointer_tail_length), angle, &tail_circle_x, &tail_circle_y);
	int tail_circle_size = static_cast<int>(6 * scale);
	for(int i = 0; i < tail_circle_size; i++)
	{
		setfillcolor(EGERGB(17+2*i,14+2*i,11+2*i));
		setcolor(EGERGB(17+2*i,14+2*i,11+2*i));
		ege_fillellipse(tail_circle_x - (tail_circle_size-i), tail_circle_y - (tail_circle_size-i), (tail_circle_size-i) * 2, (tail_circle_size-i) * 2);
	}
	
	// 绘制指针主体（白色部分）
	setfillcolor(WHITE);
	setcolor(WHITE);
	ege_point rect_points[4];
	for (int i = 0; i < 4; i++)
	{
		rect_points[i].x = rotated_rect[i][0];
		rect_points[i].y = rotated_rect[i][1];
	}
	ege_fillpoly(4, rect_points);
	
	// 绘制指针尖端（三角形）
	ege_point tri_points_ege[3];
	for (int i = 0; i < 3; i++)
	{
		tri_points_ege[i].x = rotated_tri[i][0];
		tri_points_ege[i].y = rotated_tri[i][1];
	}
	ege_fillpoly(3, tri_points_ege);
}


void drawCircle(int x, int y, int radius, int width, color_t borderColor, color_t fillColor) 
{
	x = x - radius / 3;
	y = y + radius / 2;
	// 1. 绘制边框（空心圆）
	setcolor(borderColor);
	setlinewidth(width);       // 设置边框粗细
	ege_circle(x, y, radius / 5);         // 绘制空心圆
	// 2. 绘制填充圆
	setfillcolor(fillColor);
	ege_fillcircle(x, y, (radius - width / 2) / 5); // 半径需要减去半线宽
	
	x = x + radius * 2 / 3;
	// 1. 绘制边框（空心圆）
	setcolor(borderColor);
	setlinewidth(width);       // 设置边框粗细
	ege_circle(x, y, radius / 5);         // 绘制空心圆
	// 2. 绘制填充圆
	setfillcolor(fillColor);
	ege_fillcircle(x, y, (radius - width / 2) / 5); // 半径需要减去半线宽
}

void writeWord(float x, float y, float r)
{
	setcolor(WHITE);
	LOGFONTW lf1;	// 定义 LOGFONTW 结构体变量
	ZeroMemory(&lf1, sizeof(LOGFONTW));	// 清空结构体	
	// 设置字体属性
	lf1.lfHeight = r / 6;           // 字体高度为  像素
	lf1.lfWeight = FW_BOLD;      // 字体粗细为粗体
	lf1.lfItalic = FALSE;        // 非斜体
	lf1.lfUnderline = FALSE;     // 无下划线
	lf1.lfStrikeOut = FALSE;     // 无删除线
	lstrcpyW(lf1.lfFaceName, L"黑体"); // 字体名称为黑体	
	setfont(&lf1);	// 设置字体
	ege_outtextxy((int) x -  r * 0.55, (int) y - r / 3,"INSTRUMENT AIR");
	ege_outtextxy((int) x - r / 4, (int) y + r / 6.5,"SOURCE");
	setlinestyle(SOLID_LINE, 0, r*0.0267);
	ege_line(x - 4 * r / 5, y + r / 6.5 + r / 12, x - r / 4 - 3, y + r / 6.5 + r / 12);
	ege_line(x + 4 * r / 5, y + r / 6.5 + r / 12, x + r / 4 + 3, y + r / 6.5 + r / 12);	
	lf1.lfHeight = r / 4.5;           // 字体高度为  像素
	setfont(&lf1);	// 设置字体
	ege_outtextxy(x - r / 1.5, y + r / 4,"L");
	ege_outtextxy(x +  r / 1.5 - r / 9, y + r / 4,"R");
	
	LOGFONTW lf2;	// 定义 LOGFONTW 结构体变量
	ZeroMemory(&lf2, sizeof(LOGFONTW));	// 清空结构体	
	// 设置字体属性
	lf2.lfHeight = r / 9;           // 字体高度为  像素
	lf2.lfWeight = FW_NORMAL;      // 字体粗细为正常
	lf2.lfItalic = FALSE;        // 非斜体
	lf2.lfUnderline = FALSE;     // 无下划线
	lf2.lfStrikeOut = FALSE;     // 无删除线
	lstrcpyW(lf2.lfFaceName, L"黑体"); // 字体名称为黑体	
	setfont(&lf2);	// 设置字体
	ege_outtextxy((int)x-r/2.5,(int)y, "INCHESOFMERCURY");
	lf2.lfHeight = r / 6;           // 字体高度为  像素
	lstrcpyW(lf2.lfFaceName, L"Monotype Corsiva"); // 字体名称为黑体
	setfont(&lf2);	// 设置字体	
	ege_outtextxy((int)x-r/4, (int)y+r*0.75, "Beechcraft");
	
	LOGFONTW lf3;	// 定义 LOGFONTW 结构体变量
	ZeroMemory(&lf3, sizeof(LOGFONTW));	// 清空结构体	
	// 设置字体属性
	lf3.lfHeight = r / 12;           // 字体高度为 30 像素
	lf3.lfWeight = FW_BOLD;      // 字体粗细为粗体
	lf3.lfItalic = FALSE;        // 非斜体
	lf3.lfUnderline = FALSE;     // 无下划线
	lf3.lfStrikeOut = FALSE;     // 无删除线
	lstrcpyW(lf3.lfFaceName, L"黑体"); // 字体名称为黑体	
	setfont(&lf3);	// 设置字体
	ege_outtextxy((int)x-r/6.5,(int)y+r*0.3,"MANPRESS");
	ege_outtextxy((int)x-r/20,(int)y+r*0.375,"MPD");
}

void drawDashboard(int centerX, int centerY, int radius, float angle)
{
	drawBaseDashboard(centerX, centerY, radius);
	drawScaleNum(centerX, centerY, radius);
	drawCircle(centerX, centerY, radius, radius*0.0267, WHITE, BLACK);
	writeWord(centerX, centerY, radius);
	drawPointer(centerX, centerY, radius, angle);
}

int main() 
{
	initgraph(800, 600, INIT_RENDERMANUAL);
	setbkcolor(RGB(190, 190, 190));	
	ege_enable_aa(true);
	setrendermode(RENDER_MANUAL);
	
	float centerX = 400;
	float centerY = 300;
	float radius = 150;
	float angle = 0;
	while (!kbhit()) 
	{
		cleardevice();
		// 绘制表盘
		drawDashboard(centerX, centerY, radius, angle);
		//更新角度
		angle += PI / 180;
		// 控制帧率
		delay_jfps(60);
	}	
	closegraph();
	return 0;
}